package com.example.admin.SQLite.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.example.admin.SQLite.LunTan;
import com.example.admin.SQLite.SqLiteOpenHelper;

import java.util.ArrayList;

import static com.example.admin.SQLite.SqLiteOpenHelper.FEIYONG;
import static com.example.admin.SQLite.SqLiteOpenHelper.LUNTAN;

public class LunTanDao {
    SqLiteOpenHelper dbHelper;
    private static final String KEY_ID = "id";

    public LunTanDao(Context context) {
        dbHelper = new SqLiteOpenHelper(context);
    }

    public ArrayList<LunTan> getComPlan(String uid) {
        String selection = "uid=?"; // userName为要查询的列名
        String[] selectionArgs = {uid}; // value为要查询的值
        SQLiteDatabase db = dbHelper.getSqlite();
        Cursor cursor = db.query(LUNTAN, null, selection, selectionArgs, null, null, null);

        ArrayList<LunTan> arrContacts = new ArrayList<>();

        while (cursor.moveToNext()) {
            LunTan model = new LunTan();
            model.id = cursor.getInt(0);
            model.luntanTitle = cursor.getString(1);
            model.luntanContent = cursor.getString(2);
            model.luntanTime = cursor.getString(3);
            model.luntanType = cursor.getString(4);
            model.uid = cursor.getInt(5);
            model.luntanPic = cursor.getString(6);

            arrContacts.add(model);

        }
        return arrContacts;
    }

    public ArrayList<LunTan> getComPlans() {

        SQLiteDatabase db = dbHelper.getSqlite();
        Cursor cursor = db.rawQuery("SELECT * FROM " + LUNTAN, null);

        ArrayList<LunTan> arrContacts = new ArrayList<>();

        while (cursor.moveToNext()) {
            LunTan model = new LunTan();
            model.id = cursor.getInt(0);
            model.luntanTitle = cursor.getString(1);
            model.luntanContent = cursor.getString(2);
            model.luntanTime = cursor.getString(3);
            model.luntanType = cursor.getString(4);
            model.uid = cursor.getInt(5);
            model.luntanPic = cursor.getString(6);

            arrContacts.add(model);

        }
        return arrContacts;
    }

    public long addData(LunTan bean) {
        SQLiteDatabase db = dbHelper.getSqlite();
        ContentValues values = new ContentValues();
        values.put("luntanTitle", bean.luntanTitle);
        values.put("luntanContent", bean.luntanContent);
        values.put("luntanTime", bean.luntanTime);
        values.put("luntanType", bean.luntanType);
        values.put("uid", bean.uid);
        values.put("luntanPic", bean.luntanPic);
        long id = db.insert(LUNTAN, null, values);
        Log.e("数据打印", "addData: " + bean.luntanType);
        return id;
    }

    public void DeleteContacts(int id) {
        SQLiteDatabase database = dbHelper.getSqlite();
        database.delete(LUNTAN, KEY_ID + " = ? ", new String[]{String.valueOf(id)});
    }
}
